package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.ProjectLogService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/25.
 * 项目日志
 */
@Controller
@RequestMapping(value = "/projectLogControl")
public class ProjectLogControl {
    private static Logger log = LoggerFactory.getLogger(ProjectLogControl.class);
    @Autowired
    private ProjectLogService projectLogService;

    @Autowired
    private ConsumerService consumerService;

    /**
     * 项目日志搜索
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/logList", method = RequestMethod.GET)
    public void logList(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String users = param.get("users");
            if(null == users || "".equals(users)){
                String openIdent = param.get("openIdent");
                UserEntity userEntity = UserEntity.decrypt(openIdent);
                Map<String, Object> map = consumerService.getUser(userEntity);
                users = String.valueOf(map.get("id"));
            }
            modelMap.addAttribute("logList",projectLogService.logList(param,users));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取当日项目统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/todayProSum", method = RequestMethod.GET)
    public void todayProSum(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
                String openIdent = param.get("openIdent");
                UserEntity userEntity = UserEntity.decrypt(openIdent);
                Map<String, Object> map = consumerService.getUser(userEntity);
                String id = String.valueOf(map.get("id"));
            Map<String,Object> m = projectLogService.getTodayLog(id);
            if(m==null){
                m = new HashMap<>();
                m.put("log_id","");
                m.put("user_id",id);
                m.put("voice_remark","");
                m.put("characters_remark","");
                m.put("create_time","");
            }
            m.put("todayProSum",projectLogService.todayProSum(id));
            modelMap.addAllAttributes(m);
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 项目日志填报
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/assessedLog", method = RequestMethod.POST)
    public void assessedLog(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            String user_id = String.valueOf(map.get("id"));

            Map<String,Object> m = projectLogService.getTodayLog(user_id);
            if(m==null){
                projectLogService.assessedLog(user_id,param);
            }else {
                projectLogService.upLog(m.get("log_id"),param);
            }


            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
}
